package com.cj.flipingballs;

class Wall
{
    float startX=0,starY=0,endX=0,endY=0;
}

public class Ball {
    private double x;          // X坐标（中心点）
    private double y;          // Y坐标（中心点）
    private double velocityX;  // X轴速度
    private double velocityY;  // Y轴速度
    private double radius;     // 球体半径
    private int color;       // 渲染颜色

    // 增强版构造方法
    public Ball(double x, double y, double velocityX, double velocityY, 
                double radius, int color) {
        this.x = x;
        this.y = y;
        this.velocityX = velocityX;
        this.velocityY = velocityY;
        this.radius = radius;
        this.color = color;
    }

    void setLocation(double x, double y)
    {
        this.x = x;
        this.y = y;
    }

    void move(double time)
    {
        this.x += velocityX * time;
        this.y += velocityY * time;
    }

    boolean isMoving()
    {
	    if(velocityX==0 && velocityY==0)
            return false;
	    else
            return true;
    }

    boolean collisionListenner()
    {
	    if((x + velocityX * 20 / 1000 - radius) < 0 || 
                (x + velocityX * 20 / 1000 + radius) > width)
	    {
		    v.vx*=-1;
	    }
	    if((location.y+v.vy*20/1000-radius)<0||(location.y+v.vy*20/1000+radius)>height)
	    {
		    v.vy*=-1;
	    }
	    return false;
    }

    // 精确的边界碰撞检测（考虑半径）
    public void checkBounds(double width, double height) {
        // 水平边界检测
        if (x - radius < 0) {
            x = radius;          // 将球推到边界内
            velocityX = -velocityX;
        } else if (x + radius > width) {
            x = width - radius;
            velocityX = -velocityX;
        }

        // 垂直边界检测
        if (y - radius < 0) {
            y = radius;
            velocityY = -velocityY;
        } else if (y + radius > height) {
            y = height - radius;
            velocityY = -velocityY;
        }
    }

    // 带阻尼的更新方法（deltaTime单位：秒）
    public void update(double deltaTime, double damping) {
        x += velocityX * deltaTime;
        y += velocityY * deltaTime;
        applyDamping(deltaTime, damping);
    }

    // 速度阻尼模拟
    private void applyDamping(double deltaTime, double damping) {
        double attenuation = Math.pow(damping, deltaTime);
        velocityX *= attenuation;
        velocityY *= attenuation;
    }

    // 新增Getter方法
    public Color getColor() { return color; }
    public double getRadius() { return radius; }

    // 其他原有方法保持不变，补充新的属性访问...

    @Override
    public String toString() {
        return String.format(
            "Ball[ (%.1f, %.1f) | v=%.1f, %.1f | r=%.1f | #%06x ]",
            x, y, velocityX, velocityY, radius, color.getRGB() & 0xFFFFFF
        );
    }

}
    Ball creat(float x,float y, int color)
    {
	    Ball ball=new Ball();
	    ball.location.x=x;
	    ball.location.y=y;
        ball.color = color;
	    return ball;
    }
}
